package com.weilx959.spt.plugin.e2sp.service;

import com.weilx959.spt.core.factory.SingletonFactory;
import com.weilx959.spt.core.utils.Assert;
import com.weilx959.spt.plugin.e2sp.model.ExcelVO;
import com.weilx959.spt.plugin.e2sp.utils.ResourceBundleUtil;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

import java.nio.file.Paths;
import java.util.ResourceBundle;

/**
 * 加载excel服务
 *
 * @author weilx
 * @since 2024-11-08
 */
public class LoadExcelService extends Service<ExcelVO> {
    private final ResourceBundle bundle = ResourceBundleUtil.get();
    /**
     * 文件路径
     */
    private final StringProperty filePath = new SimpleStringProperty(this, "filePath", "");

    @Override
    protected Task<ExcelVO> createTask() {
        return new LoadExcelTask();
    }

    public StringProperty filePathProperty() {
        return filePath;
    }

    private class LoadExcelTask extends Task<ExcelVO> {
        @Override
        protected ExcelVO call() throws Exception {
            String p = filePath.get();
            Assert.notBlank(p, bundle.getString("e2s.main.message.file-not-choose"));
            return SingletonFactory.getInstance(ExcelToSqlService.class).read(Paths.get(p).toFile());
        }
    }
}
